/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package centralehotelweb.bean;


import com.sun.org.apache.commons.beanutils.PropertyUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import java.util.Map;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mathieu
 */
public abstract class AbstractBean {

    protected void addMessage(Severity severity, String message) {
        getFacesContext().addMessage(null, new FacesMessage(severity, message, message));
    }
    
    protected void addMessage(String objectId,Severity severity, String message) {
        getFacesContext().addMessage(objectId, new FacesMessage(severity, message, message));
    }
    
    protected FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    protected HttpServletRequest getRequest() {
        HttpServletRequest request = (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
        return request;
    }


    /**
     * @param original collection des objets issus de la couche métier
     * @param labelProperty nom de la propriété des objets métier utilisée comme libellé dans SelectItem
     * @param valueProperty nom de la propriété des objets métier utilisée comme valeur dans SelectItem
     */
    protected List<SelectItem> buildSelectCollection(Collection<?> original, String labelProperty) {
        List<SelectItem> result = new ArrayList<SelectItem>();
        if (original != null) {
            try {
                for (Object obj : original) {
                    String label = (String) PropertyUtils.getProperty(obj, labelProperty);                    
                    SelectItem item = new SelectItem(obj, label);
                    result.add(item);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return result;
    }

    
    protected Map<String,Object> getSession()
    {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    }
    
    protected LoginBean getLoginBean(){
        return ((LoginBean)getSession().get("LoginBean"));
    }
    
    /**
     * <p>Return any attribute stored in request scope, session scope, or
     * application scope under the specified name.  If no such
     * attribute is found, and if this name is the registered name of a
     * managed bean, cause a new instance of this managed bean to be created
     * (and stored in an appropriate scope, if necessary) and returned.
     * If no attribute exists, and no managed bean was created, return
     * <code>null</code>.</p>
     *
     * @param name Name of the attribute to be retrieved
     */
    protected Object getBean(String name) {

        return getApplication().getVariableResolver().resolveVariable(getFacesContext(), name);

    }
    
    /**
     * <p>Return the <code>Application</code> instance for the current
     * web application.</p>
     */
    protected Application getApplication() {

        return FacesContext.getCurrentInstance().getApplication();

    }

}
